<?php
class HsCodesModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("hs_codes", "id");
        $this->addField("id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("subchapter", Model::TYPE_VARCHAR);
        $this->addField("section", Model::TYPE_VARCHAR);
        $this->addField("chapter", Model::TYPE_VARCHAR);
        $this->addField("hscode", Model::TYPE_VARCHAR);
        $this->addField("description_en", Model::TYPE_VARCHAR);
        $this->addField("description_hu", Model::TYPE_VARCHAR);
    }

    public function getLike($likeStr = "") {
        $query = "";
        if ($likeStr == "") {
            $query = "SELECT * FROM hs_codes ORDER BY hscode";
        } else {
            $query = "SELECT * FROM hs_codes WHERE lower(description_en) LIKE lower('%$likeStr%') OR lower(description_hu) LIKE lower('%$likeStr%') ORDER BY hscode";
        }
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function getHSLike($hslike) {
        $query= "SELECT * FROM hs_codes WHERE hscode LIKE '$hslike%' AND length(hscode) > 2 ORDER BY hscode+0";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function loadAll($condParam) {
        $result = array();
        if ($condParam != "true") {
            $query = "SELECT * FROM hs_codes WHERE chapter='$condParam' AND length(hscode) > 2 ORDER BY hscode";
            $res = Database::executeQuery($query);
            $result = Database::getDataFromResource($res);
        } else {
            $result = parent::loadAll($condParam);
        }
        return $result;
    }

    public function getHS4ByHS2Code($hs2code) {
        $query = "SELECT hscode, description_en, description_hu FROM hs_codes WHERE hscode LIKE '" . $hs2code . "__' OR hscode LIKE '" . $hs2code . "_'";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
//        if (Auth::isAdmin()) { return true; }
/*
        switch ($operation) {
            case "update" :
            case "delete" :
            case "addnew" : {
                if (Auth::canChangeDrivers()) { return true; }
            }
            case "list" : {
                if (Auth::canListDrivers()) { return true; }
            }
        }
        return false;
*/
    }

    public function modifyDatabaseInput($data) {
        $result = array();
        return $result;
    }
}

?>
